//
//  FriendViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/6/24.
//  Copyright © 2016年 XuDong Jin. All rights reserved.
//

#import "FriendViewController.h"
#import "ZiLiaoTableViewCell.h"
#import "IntroViewController.h"
#import "IGGetFocusMemberApi.h"
#import"IGArtGalleryAttentApi.h"

@interface FriendViewController () <UITableViewDelegate, UITableViewDataSource> {
    NSString *followerId;// 关注我的用户id
    NSString *focusNum;
    NSString *type;
}

@property (weak, nonatomic) IBOutlet UITableView *table;
@property(nonatomic,strong)IGFocusmember *foucusMember;
@property(nonatomic,strong)IGGetFocusMemberApi *getFocusApi;
@property (nonatomic, strong) IGArtGalleryAttentApi*attentApi;
@property (nonatomic, copy) NSString *pageNum;
@property (nonatomic, copy) NSString *pageSize;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation FriendViewController
#pragma mark-懒加载
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource=[NSMutableArray array];
    }
    return _dataSource;
}
#pragma mark-生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"我的关注";
    self.title = NSLocalizedString(@"My Follow", nil);

    self.table.backgroundColor = [UIColor c2Color];
    _pageSize = @"10";
    _pageNum = @"1";
    self.getFocusApi=[[IGGetFocusMemberApi alloc]initWithUid:[IGUser getUid] PageSize:_pageSize pageNum:_pageNum];
    self.attentApi=[[IGArtGalleryAttentApi alloc]init];
    [self.table ig_setHeaderWithRefreshingBlock:^{
        _pageSize = @"10";
        _pageNum = @"1";
        [self dataRequest];
    }];
    
    [self dataRequest];
}
-(void)dataRequest{
    self.getFocusApi.pageNum=_pageNum;
    @KWeakObj(self)
    [self.getFocusApi startWithDecodeClass:[IGGetFocusMemberResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [selfWeak.table ig_endHeaderRefreshing];
        [selfWeak.table ig_endFooterRefreshing];
        if (responseObject) {
            IGGetFocusMemberResp *resp=responseObject;
            IGGetFocusMemberModel *model=resp.data;
            if (model.focusMemberList&&model.focusMemberList.count>0) {
                [selfWeak setupFooter];
            } else
            {
                [selfWeak.table ig_endFooterRefreshingWithNoMoreData];
            }
            if ([_pageNum isEqualToString:@"1"])
            {
                _dataSource = [NSMutableArray arrayWithArray:model.focusMemberList];
            }
            else
            {
                [_dataSource addObjectsFromArray:model.focusMemberList];
            }
        
            [selfWeak.table reloadData];
        }
    }];
}
- (void)setupFooter
{
    @KWeakObj(self)
    [self.table ig_setFooterWithRefreshingBlock:^{
        _pageNum = [NSString stringWithFormat:@"%d", [_pageNum intValue] + 1];
        [selfWeak dataRequest];
    }];
}
#pragma mark -Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count>0?_dataSource.count:0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZiLiaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[ZiLiaoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.data =_dataSource[indexPath.row];
    [cell followOwerAction:^(IGFocusmember *info) {
        followerId = info.otherId;
        if ([info.attentionState integerValue]==0) { //是否 关注  0=已关注，1=未关注
            // 取消关注
            type = @"1";
            _attentApi.museumId=followerId;
            _attentApi.operatorType=type;
            [self.attentApi startWithDecodeClass:[IGArtGalleryAttentResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
                if (responseObject) {
//                    [self presentMessageTips:@"取消成功"];
//                    [self presentMessageTips:NSLocalizedString(@"Canceled Successfully", nil)];
                    [self.dataSource removeObjectAtIndex:indexPath.row];
                    [self.table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                    [self.table reloadData];
                }
            }];
        }
    }];
    [cell setNeedsLayout];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZiLiaoTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    IGFocusmember *info = cell.data;
    IntroViewController *vc = [[IntroViewController alloc] init];
    vc.u_id = info.otherId;
    [UIViewController ig_pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
